<template>
  <div>
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="销售单号" prop="salesOrderNumber">
        <el-input v-model="formData.salesOrderNumber" placeholder="请输入销售单号" clearable
                  :style="{width: '300px'}"></el-input>
      </el-form-item>
      <el-form-item label="销售机器" prop="salesMachineNumberId">
        <el-select v-model="formData.salesMachineNumberId" placeholder="请选择销售机器" clearable
                   :style="{width: '300px'}">
          <el-option v-for="(item, index) in salesMachineNumberIdOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品编号" prop="salesProductId">
        <el-input v-model="formData.salesProductId" placeholder="请输入商品编号" clearable :style="{width: '300px'}">
        </el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="salesProductQuantity">
        <el-input v-model.number="formData.salesProductQuantity" placeholder="请输入商品数量" clearable
                  :style="{width: '300px'}" @input="calculateTotal"></el-input>
      </el-form-item>
      <el-form-item label="出售单价" prop="salesSellingPrice">
        <el-input v-model.number="formData.salesSellingPrice" placeholder="请输入出售单价" clearable
                  :style="{width: '300px'}" @input="calculateTotal"></el-input>
      </el-form-item>
      <el-form-item label="总金额" prop="salesTotal">
        <el-input v-model="formData.salesTotal" placeholder="自动计算总金额" :style="{width: '300px'}" disabled>
        </el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="salesMember">
        <el-input v-model="formData.salesMember" placeholder="请输入手机号" :maxlength="11" show-word-limit
                  clearable prefix-icon='el-icon-mobile' :style="{width: '300px'}"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="salesCreationTime">
        <el-date-picker
          v-model="formData.salesCreationTime"
          type="datetime"
          placeholder="选择日期时间"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          :style="{width: '300px'}"
          disabled>
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人编号" prop="salesCreatorId">
        <el-input v-model="formData.salesCreatorId" placeholder="请输入创建人编号" clearable :style="{width: '300px'}">
        </el-input>
      </el-form-item>
      <el-form-item size="large">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        salesOrderNumber: '',
        salesMachineNumberId: undefined,
        salesProductId: undefined,
        salesProductQuantity: undefined,
        salesSellingPrice: undefined,
        salesTotal: undefined,
        salesMember: '',
        salesCreationTime: new Date().toISOString().replace('T', ' ').substring(0, 19),
        salesCreatorId: undefined,
      },
      rules: {
        salesOrderNumber: [{
          required: true,
          message: '请输入销售单号',
          trigger: 'blur'
        }],
        salesMachineNumberId: [{
          required: true,
          message: '请选择销售机器',
          trigger: 'change'
        }],
        salesProductId: [{
          required: true,
          message: '请输入商品编号',
          trigger: 'blur'
        }],
        salesProductQuantity: [{
          required: true,
          message: '请输入商品数量',
          trigger: 'blur'
        }, {
          type: 'number',
          message: '商品数量必须为数字',
          trigger: 'blur'
        }],
        salesSellingPrice: [{
          required: true,
          message: '请输入出售单价',
          trigger: 'blur'
        }, {
          type: 'number',
          message: '出售单价必须为数字',
          trigger: 'blur'
        }],
        salesMember: [{
          pattern: /^1(3|4|5|7|8|9)\d{9}$/,
          message: '手机号格式错误',
          trigger: 'blur'
        }],
        salesCreatorId: [{
          required: true,
          message: '请输入创建人编号',
          trigger: 'blur'
        }],
      },
      salesMachineNumberIdOptions: [{
        "label": "销售机器1",
        "value": 1
      }, {
        "label": "销售机器2",
        "value": 2
      }, {
        "label": "销售机器3",
        "value": 3
      }],
    }
  },
  methods: {
    calculateTotal() {
      if (this.formData.salesProductQuantity && this.formData.salesSellingPrice) {
        this.formData.salesTotal = (this.formData.salesProductQuantity * this.formData.salesSellingPrice).toFixed(2);
      } else {
        this.formData.salesTotal = undefined;
      }
    },
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return;
        addSale(this.formData).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.resetForm();
        });
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields();
      // 重置后重新设置当前时间
      this.formData.salesCreationTime = new Date().toISOString().replace('T', ' ').substring(0, 19);
    },
  }
}

export function addSale(data) {
  return request({
    url: '/sale/sale',
    method: 'post',
    data: data
  })
}
</script>

<style>
</style>
